import Cookies from 'js-cookie'
import { setSessionStoreage, getSessionStoreage } from './index'

const TokenKey = 'Admin-Token'
const UserInfoKey = 'admin-userinfo'

export function getToken() {
  return Cookies.get(TokenKey)
}

export function setToken(token) {
  return Cookies.set(TokenKey, token)
}

export function removeToken() {
  return Cookies.remove(TokenKey)
}

export function getUserInfo() {
  let userInfo = getSessionStoreage(UserInfoKey)
  return userInfo ? JSON.parse(userInfo) : {}
}

export function setUserInfo(userinfo) {
  return setSessionStoreage(UserInfoKey, JSON.stringify(userinfo))
}

export function removeUserInfo() {
  return setSessionStoreage(UserInfoKey, '')
}

export function authRouter(routerArr, path) {
  let b = false
  if (routerArr && routerArr.length && path) {
    for (let i = 0; i < routerArr.length; i++) {
      if (routerArr[i].name === path) {
        b = true
      } else {
        b = authRouter(routerArr[i].children, path)
      }
      if (b) {
        return b
      }
    }
  }
  return b
}
